package com.orange.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import server.rest.ContentType;
import server.rest.HttpContext;
import server.rest.HttpMethod;
import server.rest.ResponseException;
import server.rest.Rest;
import server.rest.filter.Filter;
import com.orange.api.dto.FeatureDto;
import com.orange.api.dto.NavExample;
import common.ParamHelper;

@Filter(name = "CrossDomainAccess", filter = "com.orange.api.filter.TestClassFilter")
public class FeatureHandler
{

    public FeatureHandler()
    {
    }

    static List<FeatureDto> LIST = new ArrayList<FeatureDto>();

    static
    {
        LIST.add(new FeatureDto(1, "张三", "123456", "我从东方走来，要去西天取经！", new Date()));
        LIST.add(new FeatureDto(2, "张四", "9521324", "白日依山尽，黄河不到头", new Date()));
        LIST.add(new FeatureDto(3, "王五", "3334544", "小宇宙爆发，天马流星拳。", new Date()));
    }

    @Filter(name = "Pagination", filter = "com.orange.api.filter.PaginationFilter")
    @Rest(method = HttpMethod.GET, schema = "/users", internal = false, responseContentType = ContentType.JSON ,appSchame="/page")
    public List<FeatureDto> doGetAllUsers(final HttpContext context) throws ResponseException
    {
        // return LIST.subList(Math.min((page - 1) * rows, LIST.size()), Math.min(page * rows,
        // LIST.size()));
        return LIST;
    }

    @Rest(method = HttpMethod.GET, schema = "/user/(userId)", internal = false, responseContentType = ContentType.JSON)
    public FeatureDto doGetClientUser(final HttpContext context) throws ResponseException
    {
        int id = ParamHelper.getRequiredInt(context, "userId");
        for (FeatureDto dto : LIST)
        {
            if (id == dto.getId())
            {
                return dto;
            }
        }
        throw new ResponseException(HttpServletResponse.SC_NOT_FOUND, "No found UserId : " + id);
    }

    @Rest(method = HttpMethod.POST, schema = "/user/(userId)", internal = false, requestContentType = ContentType.JSON, responseContentType = ContentType.JSON)
    public FeatureDto doPostClientUser(final HttpContext context, FeatureDto dto) throws ResponseException
    {
        int id = ParamHelper.getRequiredInt(context, "userId");

        for (FeatureDto featureDto : LIST)
        {
            if (id == featureDto.getId())
            {
                throw new ResponseException(HttpServletResponse.SC_BAD_REQUEST, "");
            }
        }
        LIST.add(dto);
        return dto;
    }

    @Rest(method = HttpMethod.PUT, schema = "/user/(userId)", internal = false, requestContentType = ContentType.JSON, responseContentType = ContentType.JSON)
    public FeatureDto doPutClientUser(final HttpContext context, FeatureDto dto) throws ResponseException
    {
        int id = ParamHelper.getRequiredInt(context, "userId");

        for (FeatureDto featureDto : LIST)
        {
            if (id == featureDto.getId())
            {
                featureDto.setDesc(dto.getDesc());
                featureDto.setDesc(dto.getDesc());
                featureDto.setDesc(dto.getDesc());
                featureDto.setDesc(dto.getDesc());
                return featureDto;
            }
        }

        return dto;
    }
    
    /*
     * navigate example
     */
    @Rest(method = HttpMethod.GET, schema = "/bo/nav", internal = false, responseContentType = ContentType.JSON)
    public List<NavExample> getNavExample(final HttpContext context) throws Exception
    {
        List<NavExample> response = new ArrayList<NavExample>();
        List<NavExample> childrenList = new ArrayList<NavExample>();
        NavExample ex = new NavExample();
        ex.setId(1);
        ex.setText("测试");
        ex.setIcon("icon-dashboard");

        NavExample ex1 = new NavExample();
        ex1.setId(2);
        ex1.setText("班级");
        ex1.setValue("/demo/dyc.class.html");
        ex1.setIcon("icon-columns");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(3);
        ex1.setText("学校");
        ex1.setValue("/demo/dyc.school.html");
        ex1.setIcon("icon-sitemap");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(4);
        ex1.setText("教师");
        ex1.setValue("/demo/dyc.teacher.html");
        ex1.setIcon("icon-user");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(5);
        ex1.setText("科目");
        ex1.setValue("/demo/dyc.subject.html");
        ex1.setIcon("icon-list-alt");
        childrenList.add(ex1);

        ex.setChildren(childrenList);
        response.add(ex);

        ex = new NavExample();
        ex.setId(1);
        ex.setText("测试2");
        ex.setIcon("icon-coffee");

        childrenList = new ArrayList<NavExample>();

        ex1 = new NavExample();
        ex1.setId(2);
        ex1.setText("班级 2");
        ex1.setValue("/demo/dyc.class.html");
        ex1.setIcon("icon-columns");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(3);
        ex1.setText("学校 2");
        ex1.setValue("/demo/dyc.school.html");
        ex1.setIcon("icon-sitemap");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(4);
        ex1.setText("  教师 2");
        ex1.setValue("/demo/dyc.teacher.html");
        ex1.setIcon("icon-user");
        childrenList.add(ex1);
        ex1 = new NavExample();
        ex1.setId(5);
        ex1.setText("科目  2");
        ex1.setValue("/demo/dyc.subject.html");
        ex1.setIcon("icon-list-alt");
        childrenList.add(ex1);

        ex.setChildren(childrenList);
        response.add(ex);

        return response;
    }

}
